/**
 * @Description 基础框架服务调用实例-报表服务
 * @Author  guoyanfei
 */
var editState = 0;
var commServices = commServices || {};
var serviceUrl = '/gdp'
commServices.prototype = {
    init: function () {//初始化
        commServices.prototype.setSearchColumn();
        commServices.prototype.eventSearch();
        commServices.prototype.setIframeHeight(document.getElementById('sysIframe'));
        commServices.prototype.reportView();
    },
    reportView: function() {
    	var parm = commServices.prototype.queryParams();
    	$("#sysIframe").attr("src",serviceUrl+"/business/reportServer/reportView?serviceID="+serviceId+"&conQuery="+parm.conQuery);
    	$("#preview_url").html(serviceUrl+"/business/reportServer/reportView?serviceID="+serviceId+"&conQuery="+parm.conQuery);
    },
    reportExport: function(act) {
    	var parm = commServices.prototype.queryParams();
    	window.location.href=serviceUrl+"/business/reportServer/reportExport?serviceID="+serviceId+"&style="+ act +"&conQuery="+parm.conQuery;
    },
    queryParams: function () {//表格参数设置
        var temp = {
            serviceID: serviceId,
//            pageNO: pageReqeust.offset / pageReqeust.limit + 1,
//            pageSize: pageReqeust.limit,
            conQuery: commServices.prototype.setSearchParams()
        };
        return temp;
    },
    setSearchColumn:function(){//设置查询列
        var columnData = new Array();
        $.ajax({
            url: serviceUrl + '/business/reportServer/queryReportQueryParams',
            type: 'get',
            async: false,
            data: { serviceID: serviceId },
            dataType: "json",
            success: function (data) {
                columnData = data.data;
            },
            error: function () {
                bootbox.alert("列数据加载失败!");
            }
        });
        if (columnData != null) {
            $.each(columnData, function (i, e) {
            	var fieldname = "search"+e.fieldname;
            	fieldname = fieldname.replace("@","_");
                switch (e.showtype) {
                    case "0"://文本框
                        $("#txtSearch").append("<label style='margin-left:10px;' for='" + fieldname + "'>" + e.fieldnamecn + "：</label>");
                        $("#txtSearch").append("<input id='" + fieldname + "' placeholder='请输入" + e.fieldnamecn + "' type='text' class='form-control' >");
                        break;
                    case "1"://下拉框
                        $("#txtSearch").append("<label style='margin-left:10px;' for='" + fieldname + "'>" + e.fieldnamecn + "：</label>");
                        var strtxt = "<select id='" + fieldname + "'  class='form-control' >"
                                       + "<option value=''>请选择</option>";
                        if (e.enumvalue != null && e.enumvalue != "") {
                            var enums = e.enumvalue.split(',');
                            for (var i = 0; i < enums.length; i++) {
                                var eb = enums[i].split(':');
                                strtxt += "<option value='"+eb[0]+"'>"+eb[1]+"</option>";
                            }
                        }
                        strtxt += "</select>";
                        $("#txtSearch").append(strtxt);
                        break;
                    case "4"://日期框
                        $("#txtSearch").append("<label style='margin-left:10px;' for='" + fieldname + "'>" + e.fieldnamecn + "：</label>");
                        $("#txtSearch").append("<input id='" + fieldname + "' data-date-format=\"yyyy-mm-dd\" type='text' class='form-control' >");
                        $("#" + fieldname + "").datetimepicker();
                        break;
                }
            });
            
        }
    },
    setSearchParams:function(){//设置查询参数
        var searchParams = "";
        var columnData = new Array();
        $.ajax({
            url: serviceUrl + '/business/reportServer/queryReportQueryParams',
            type: 'get',
            async: false,
            data: { serviceID: serviceId },
            dataType: "json",
            success: function (data) {
                columnData = data.data;
            },
            error: function () {
                bootbox.alert("列数据加载失败!");
            }
        });
        if (columnData != null) {
            $.each(columnData, function (i, e) {
            	var fieldname = "search"+e.fieldname;
            	fieldname = fieldname.replace("@","_");
                if ($('#' + fieldname).val() != null && $('#' + fieldname).val() != 'undefined' && $('#' + fieldname).val() != "") {
                	  if (searchParams == "") {
                          searchParams = "\"" + e.fieldname + "\":\""+ $('#' + fieldname).val()+"\"";
                      } else { 
                          searchParams += ",\"" + e.fieldname + "\":\"" + $('#' + fieldname).val() + "\"";
                      }
                }
            });
            searchParams = "%7B" + searchParams + "%7D";
        }
        return searchParams;
    },
    eventSearch:function(){//查询操作
        $("#btn_search").click(function () {
        	commServices.prototype.reportView();
        });
    },
    setIframeHeight:function(iframe) {
    	if (iframe) {
    		var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
    			if (iframeWin.document.body) {
    			/* iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight; */
    			iframe.height = document.documentElement.clientHeight-iframeWin.document.documentElement.scrollHeight;
    			}
    		}
    },

};

//服务初始化
$(function () {
    $("#sp_seviceUrl").append("服务地址：" + serviceUrl + "/business/reportServer/reportView");
    $("#sp_seviceUrl").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;服务ID：" + serviceId);
    
    var comm = commServices.prototype;
    comm.init();
})

